#include"stack.h"

//API
seqstack *init_stack(int cap){
    seqstack *sq = malloc(sizeof(seqstack));
    if(sq != NULL){
        sq->stack = malloc(sizeof(char) * cap);
        if(sq->stack == NULL){
            free(sq);
            return NULL;
        }
        sq->size = cap;
        sq->top = -1;
    }
    return sq;
}

bool isfull(seqstack *sq){
    return sq->top == sq->size-1;
}

bool isempty(seqstack *sq){
    return sq->top == -1;
}

//入栈
bool push(seqstack *sq, char data){
    if(sq != NULL && !isfull(sq)){
        sq->stack[++sq->top] = data;
        return true;
    }
    return false;
}

//出栈
bool pop(seqstack *sq, char *data){
    if(sq != NULL && !isempty(sq)){
        *data = sq->stack[sq->top--];
        return true;
    }
    return false;
}

//取栈顶,获取栈顶元素值
bool top(seqstack *sq, char *pdata){
    if(sq != NULL && !isempty(sq)){
        *pdata = sq->stack[sq->top];
        return true;
    }
    return false;
}